<?php
/**
 * Attribute Aggregator Authentication Processing filter
 *
 * Filter for requesting the vo to give attributes to the SP.
 *
 * @author Gyula Szabó <gyufi@sztaki.hu>
 * @package simpleSAMLphp
 * @version $Id$
 */
class sspmod_attributeaggregator_Auth_Process_attributeaggregator extends SimpleSAML_Auth_ProcessingFilter
{

	/**
	 *
	 * AA IdP entityId
	 * @var string
	 */
	private $_entityId = null;	

	/**
	 * 
	 * eduPersonPrincipalName, the key of the user in the AA
	 * @var unknown_type
	 */
	private $_eppn = null;

	/**
	 * Initialize attributeaggregator filter
	 *
	 * Validates and parses the configuration
	 *
	 * @param array $config   Configuration information
	 * @param mixed $reserved For future use
	 */
	public function __construct($config, $reserved)
	{
		assert('is_array($config)');
		parent::__construct($config, $reserved);

		$metadata = SimpleSAML_Metadata_MetaDataStorageHandler::getMetadataHandler();

		if (array_key_exists('entityId', $config)) {
			$aameta = $metadata->getMetaData($config['entityId'], 'attributeauthority-remote');
			if (!$aameta) {
				throw new SimpleSAML_Error_Exception(
                    'attributeaggregator: AA entityId must be valid. ' .
				var_export($config['entityId']) . ' given.'
				);
			}
			$this->_entityId = $config['entityId'];
		}		
	}

	/**
	 * Process a authentication response
	 *
	 * This function saves the state, and redirects the user to the Attribute Authority for
	 * entitlements. 
	 *
	 * @param array &$state The state of the response.
	 *
	 * @return void
	 */
	public function process(&$state)
	{
		assert('is_array($state)');		
		$state['attributeaggregator:eppn'] = $state['Attributes']['urn:oid:1.3.6.1.4.1.5923.1.1.1.6'];
		$state['attributeaggregator:entityId'] = $this->_entityId;
		if (! $state['attributeaggregator:eppn']){
			throw new SimpleSAML_Error_Exception("This user has no eppn! She has to have.");
		}

		// Save state and redirect
		$id  = SimpleSAML_Auth_State::saveState($state, 'attributeaggregator:request');
		$url = SimpleSAML_Module::getModuleURL('attributeaggregator/attributequery.php');
		SimpleSAML_Utilities::redirect($url, array('StateId' => $id));
	}
}
